package com.wangfugui.mybatisplus.common;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

@Data
@ApiModel("分页请求基类")
public class PageRequest {

    @ApiModelProperty(value = "当前页码", example = "1")
    private Integer pageNum = 1;

    @ApiModelProperty(value = "每页数量", example = "10")
    private Integer pageSize = 10;

    @ApiModelProperty(value = "排序字段")
    private String sortField;

    @ApiModelProperty(value = "排序方式(asc/desc)")
    private String sortOrder;

    /**
     * 获取MyBatis-Plus的Page对象
     */
    public <T> Page<T> toPage() {
        Page<T> page = new Page<>(pageNum, pageSize);
        if (StringUtils.isNotBlank(sortField)) {
            boolean isAsc = "asc".equalsIgnoreCase(sortOrder);
            page.addOrder(isAsc ? OrderItem.asc(sortField) : OrderItem.desc(sortField));
        }
        return page;
    }
}